const scanner = require("../supports/scanner")


module.exports = class HandlerManager {
  static handlerList = []       // method: "*"

  add(handler) {
    HandlerManager.handlerList.push(handler)
  }

  getHandler(req) {
    return HandlerManager.handlerList.find(item => item.method === req.method && req.url === item.path)
  }

  constructor(path) {
    this.scan(path)
    console.log(HandlerManager.handlerList)
    //const routes = HandlerManager.handlerList.map(item => ({
    //  path: item.path,
    //  method: item.method
    //}))
  }

  scan(path) {
    scanner(path, /.*Controller\.js/, {handler: file => {
        console.log("load controller file:", file)
        require(file)
      }})
  }
}
